package com.example.userpointmgr.service;

import org.apache.tomcat.util.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

//要收费
public class WeChatDecrypt {
    public static String decryptPhoneNumber(String encryptedData, String sessionKey, String iv) {
        try {
            byte[] encryptedDataBytes= Base64.decodeBase64(encryptedData);
            byte[] sessionKeyBytes= Base64.decodeBase64(sessionKey);
            byte[] ivBytes= Base64.decodeBase64(iv);
            SecretKeySpec secretKeySpec = new SecretKeySpec(sessionKeyBytes, "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE,secretKeySpec,new IvParameterSpec(ivBytes));
            byte[] decryptedDataBytes = cipher.doFinal(encryptedDataBytes);
            return new String(decryptedDataBytes);
        }catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
